<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>文件分片上传示例</title>
    <link rel="stylesheet" href="http://zui.sexy/dist/css/zui.min.css" rel="external nofollow">
</head>

<body>
    <div class="container" style="margin-top: 30px;">
        <form class="form-inline" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <input type="file" id="fileBig" class="form-control">
            </div>
            <button type="button" class="btn btn-primary">提交</button>
        </form>
    </div>
  
    <script src="http://zui.sexy/assets/jquery.js"></script>
    <script src="http://zui.sexy/dist/js/zui.min.js"></script>
    
    <script type="text/javascript">







           $.ajax({
                url: "http://csphoca.aiitle.com:99/v1/home/discover",
                type: "POST",
                data: '32423324',
                success: (res) => {

                   
                }
            })







        // ID
        function genID(length)
        {
            return Number(Math.random().toString().substr(3,length) + Date.now()).toString(36);
        }

        $('.btn-primary').click(function () 
        {
            // 文件详情
            let file = $("#fileBig")[0].files[0];
            let fileName = file.name;
            let fileSize = file.size;


            let blockSize = 0.9 * 1024 * 1024;
            let totalNum = Math.ceil(fileSize / blockSize);
            let start = 0;
            let end = 0;

            // 随机ID
            let uuid = genID();


            request(1, blockSize, file, fileSize, totalNum, start, uuid, fileName);
        });



        function request(index, blockSize, file, fileSize, totalNum, start, uuid, fileName)
        {
            if(index > totalNum)
            {
                return false;
            }

            end = blockSize * index;

            // 校验是否超过文件大小
            if (end > fileSize) 
            {
                end = fileSize;
            }

            // 文件块拆分
            let block = file.slice(start, end);
            start = end;
            let fd = new FormData();

            fd.append('uuid', uuid);
            fd.append('file', block);
            fd.append('fileName', fileName);
            fd.append('num', index);
            fd.append('totalNum', totalNum);
            fd.append('fileSize', fileSize);
            
            $.ajax({
                url: "/api/index/upload/",
                type: "POST",
                data: fd,
                async: true,
                processData: false,
                contentType: false,
                success: (res) => {

                    console.log(res);

                    if(-1 == res)
                    {
                        index = index + 1;

                        arguments.callee(index, blockSize, file, fileSize, totalNum, start, uuid, fileName);
                    }

                    if(1 == res)
                    {
                        alert('ok');
                    }
                }
            })
        }

    </script>
</body>

</html>